﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace Obsidian.Input
{
    internal class InputAxesMap
    {
        /// <summary>
        /// The name of the string used to check this axis from a script.
        /// </summary>
        public string Name;

        /// <summary>
        /// The button used to push the axis in the negative direction.
        /// </summary>
        public ObsidianButton NegativeButton;

        /// <summary>
        /// The button used to push the axis in the positive direction.
        /// </summary>
        public ObsidianButton PositiveButton;

        /// <summary>
        /// 	Speed in units per second that the axis falls toward neutral when no buttons are pressed.
        /// </summary>
        public float Gravity;

        /// <summary>
        /// 	Size of the analog dead zone. All analog device values within this range result map to neutral.
        /// </summary>
        public float Dead;

        /// <summary>
        /// 	Speed in units per second that the the axis will move toward the target value. This is for digital devices only.
        /// </summary>
        public float Sensitivity;

        /// <summary>
        /// The type of inputs that will control this axis.
        /// </summary>
        public InputType Type;

        // holds the current value of the axis
        internal float Value;

        internal static ButtonMapper buttonMap;

        static InputAxesMap()
        {
            buttonMap = new ButtonMapper();
        }

        // called if Type is MouseMovement or KeyOrMouseButton
        // only does mouse processing
        internal void Update(MouseState previous, MouseState current)
        {
            // handle mouse buttons a little different
            switch (PositiveButton)
            {
                case ObsidianButton.LeftMouseButton:
                    if (previous.LeftButton == ButtonState.Pressed && current.LeftButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.LeftButton == ButtonState.Released && current.LeftButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.RightMouseButton:
                    if (previous.RightButton == ButtonState.Pressed && current.RightButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.RightButton == ButtonState.Released && current.RightButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.MiddleMouseButton:
                    if (previous.MiddleButton == ButtonState.Pressed && current.MiddleButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.MiddleButton == ButtonState.Released && current.MiddleButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.MouseX:
                    break;
                case ObsidianButton.MouseY:
                    break;
                case ObsidianButton.MouseW:
                    break;
                default:
                    // mouse button isn't a button
                    // throw an exception?
                    break;
            }

            switch (NegativeButton)
            {
                case ObsidianButton.LeftMouseButton:
                    if (previous.LeftButton == ButtonState.Pressed && current.LeftButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.LeftButton == ButtonState.Released && current.LeftButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.RightMouseButton:
                    if (previous.RightButton == ButtonState.Pressed && current.RightButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.RightButton == ButtonState.Released && current.RightButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.MiddleMouseButton:
                    if (previous.MiddleButton == ButtonState.Pressed && current.MiddleButton == ButtonState.Released)
                    {
                        // mouse button up
                    }
                    else if (previous.MiddleButton == ButtonState.Released && current.MiddleButton == ButtonState.Pressed)
                    {
                        // mouse button down
                    }
                    break;
                case ObsidianButton.MouseX:
                    break;
                case ObsidianButton.MouseY:
                    break;
                case ObsidianButton.MouseW:
                    break;
                default:
                    // mouse button isn't a button
                    // throw an exception?
                    break;
            }
        }

        // called if Type KeyOrMouseButton
        // however only does key processing
        internal void Update(KeyboardState previous, KeyboardState current)
        {
            // Positive 
            if (previous.IsKeyDown((Keys)buttonMap[PositiveButton]) && current.IsKeyUp((Keys)buttonMap[PositiveButton]))
            {
                // key up
            }
            else if (previous.IsKeyUp((Keys)buttonMap[PositiveButton]) && current.IsKeyDown((Keys)buttonMap[PositiveButton]))
            {
                // key down
            }

            // Negative 
            if (previous.IsKeyDown((Keys)buttonMap[NegativeButton]) && current.IsKeyUp((Keys)buttonMap[NegativeButton]))
            {
                // key up
            }
            else if (previous.IsKeyUp((Keys)buttonMap[NegativeButton]) && current.IsKeyDown((Keys)buttonMap[NegativeButton]))
            {
                // key down
            }
        }

        // called if Type JoystickAxes
        // only does joystick processing
        internal void Update(GamePadState previous, GamePadState current)
        {
            // TODO !!!!
        }
    }

    public enum InputType
    {
        KeyOrMouseButton,
        MouseMovement,
        JoystickAxes,
    }
}
